package com.yesorno.activity;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import android.content.Intent;
import android.os.Bundle;
import android.view.Gravity;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.AdapterView.OnItemSelectedListener;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.LinearLayout.LayoutParams;
import android.widget.ListView;
import android.widget.ScrollView;
import android.widget.Spinner;
import android.widget.TextView;

import com.yesorno.constants.Constants;
import com.yesorno.model.HttpResponseObject;
import com.yesorno.ui.CustomTitleActivity;
import com.yesorno.ui.MyArrayAdapter;
import com.yesorno.ui.MyMenuList;
import com.yesorno.ui.TopicLinerLayout;
import com.yesorno.utils.HttpUtils;

public class HallActivity extends CustomTitleActivity {

	private Button publish;// 发布
	private Button refresh;// 刷新
	private Button hall_btn_submit;// 确认

	private Spinner spinner;

	private ScrollView hall_lanmu;

	private LinearLayout type_area_1;

	int rowCount = 4;// 多选区域每行4个

	private ListView infoListView;
	private ListView hall_list;

	HallBtnsClickListeners rbc = new HallBtnsClickListeners();

	/**
	 * 大厅页面按钮的监听类
	 * 
	 * @author samoin
	 * @date 2011-07-06
	 * @version 0.1
	 * 
	 */
	class HallBtnsClickListeners implements OnClickListener {
		@Override
		public void onClick(View v) {
			if (v == publish) {
				Intent intent = new Intent();
				intent.setClass(HallActivity.this, PublishTopicActivity.class);
				startActivity(intent);
			}
			if (v == hall_btn_submit) {
				hall_list.setVisibility(View.VISIBLE);
				hall_lanmu.setVisibility(View.GONE);
				List<Map<String, Object>> mapList = null;
				try {
					mapList = new ArrayList<Map<String, Object>>();
					HttpResponseObject hro = HttpUtils.getInfoByUrlPost(
							Constants.HALL_LIST_URL + "userId="
									+ Constants.USER_ID, HallActivity.this);
					JSONArray array = hro.getResult().getJSONArray("list");
					for (int i = 0; i < array.length(); i++) {
						JSONObject jsonObject = array.getJSONObject(i);
						Map<String, Object> map = new HashMap<String, Object>();
						map.put("author_logo", jsonObject.getString("userLogo"));
						map.put("author_id", jsonObject.getLong("userId"));
						map.put("author_name", jsonObject.getString("userName"));
						map.put("publish_time",
								jsonObject.getString("publishTime"));
						map.put("publish_info", jsonObject.getString("desc"));
						map.put("publish_logo", jsonObject.getString("img"));
						map.put("publish_from",
								Constants.INFO_FROM_MAP.get(""
										+ jsonObject.getInt("from")));
						map.put("ticket_count_yes",
								jsonObject.getInt("countYes") + "票");
						map.put("ticket_count_no", jsonObject.getInt("countNo")
								+ "票");
						map.put("publish_info_id",
								jsonObject.getLong("topicId"));
						mapList.add(map);
					}

				} catch (JSONException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
				ImgSimpleAdapter adapter = new ImgSimpleAdapter(
						HallActivity.this, mapList, R.layout.hall_list,
						new String[] { "author_logo", "author_name",
								"publish_time", "publish_info", "publish_logo",
								"publish_from", "ticket_count_yes",
								"ticket_count_no", "publish_info_id",
								"author_id" }, new int[] { R.id.author_logo,
								R.id.author_name, R.id.publish_time,
								R.id.publish_info, R.id.publish_logo,
								R.id.publish_from, R.id.ticket_count_yes,
								R.id.ticket_count_no, R.id.publish_info_id,
								R.id.author_id });
				infoListView.setAdapter(adapter);
				infoListView
						.setOnItemClickListener(new MyOnItemClickListener());
			}
		}
	}

	// 点击大厅列表中的某行
	class MyOnItemClickListener implements OnItemClickListener {

		@Override
		public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
				long arg3) {
			Intent intent = new Intent();
			TextView idTv = (TextView) arg1.findViewById(R.id.publish_info_id);
			TextView idTv2 = (TextView) arg1.findViewById(R.id.author_id);
			intent.putExtra("currentTopicId", Long.valueOf(idTv.getText() + ""));
			intent.putExtra("currentUserId", Long.valueOf(idTv2.getText() + ""));
			intent.setClass(HallActivity.this, HallMenuActivity.class);
			startActivity(intent);
		}

	}

	@Override
	public void setTitleViews() {
		LinearLayout titleLayout = getTITLE_LAYOUT();

		publish = CustomTitleActivity.createTitleLeftButton(HallActivity.this,
				R.string.hall_btn_publish, Constants.TITLE_BTN_MARGIN1);
		publish.setOnClickListener(rbc);
		titleLayout.addView((View) publish.getParent());

		TextView tv = CustomTitleActivity.createTitleDesc(HallActivity.this,
				R.string.hall_title, 0);
		titleLayout.addView((View) tv.getParent());

		refresh = CustomTitleActivity.createTitleRightButton(HallActivity.this,
				R.string.hall_btn_refresh, Constants.TITLE_BTN_MARGIN2);
		LinearLayout ll = (LinearLayout) refresh.getParent();
		ll.setGravity(Gravity.RIGHT);
		refresh.setOnClickListener(rbc);
		titleLayout.addView(ll);

	}

	@Override
	public void setLayoutId() {
		setContentView(R.layout.hall);
	}

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		spinner = (Spinner) findViewById(R.id.hall_spinner);
		hall_lanmu = (ScrollView) findViewById(R.id.hall_lanmu);
		hall_list = (ListView) findViewById(R.id.hall_list);
		hall_btn_submit = (Button) findViewById(R.id.hall_btn_submit);
		hall_btn_submit.setOnClickListener(rbc);
		@SuppressWarnings("rawtypes")
		ArrayAdapter adapter = MyArrayAdapter.createFromResource(this,
				R.array.hall_types, R.layout.my_spinner);
		adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
		spinner.setAdapter(adapter);
		spinner.setOnItemSelectedListener(new MyOnItemSelectedListener());

		((LinearLayout) findViewById(R.id.menus)).addView(new MyMenuList(
				HallActivity.this, 0));

		infoListView = (ListView) findViewById(R.id.hall_list);

		// 这句代码非常重要，用来设置点击的当前项的背景
		infoListView.setSelector(R.drawable.list_bg);
		// 这句代码非常重要，用来设置拖动中的背景
		infoListView.setCacheColorHint(0);

		type_area_1 = (LinearLayout) findViewById(R.id.type_area_1);
		// TODO 从网络获取所有的分类

		HttpResponseObject hro = HttpUtils.getInfoByUrlPost(
				Constants.USER_CATEGORY_LIST_URL + "userId="
						+ Constants.USER_ID, HallActivity.this);
		JSONArray array = null;
		try {
			array = hro.getResult().getJSONArray("list");
		} catch (JSONException e) {
			e.printStackTrace();
		}
		LinearLayout llTemp = null;
		for (int i = 0; i < array.length(); i++) {
			if (i % rowCount == 0) {
				llTemp = new LinearLayout(HallActivity.this);
				LinearLayout.LayoutParams lp = new LayoutParams(
						LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT, 1);
				llTemp.setLayoutParams(lp);
				type_area_1.addView(llTemp);
			}
			try {
				JSONObject obj = (JSONObject) array.get(i);
				String id = obj.getString("id");
				String name = obj.getString("name");
				Boolean isSelect = obj.getBoolean("isSelect");
				TopicLinerLayout tll = new TopicLinerLayout(HallActivity.this,
						Constants.TOPIC_TYPE_MAP.get(id), name,
						Integer.valueOf(id));
				llTemp.addView(tll);
				if (isSelect) {
					tll.checkBox.setChecked(true);
				}
			} catch (JSONException e) {
				e.printStackTrace();
			}
		}
		// Display display = getWindowManager().getDefaultDisplay();
		// System.out
		// .println(infoListView.getHeight() + ":" + display.getHeight());
	}

	class MyOnItemSelectedListener implements OnItemSelectedListener {

		@Override
		public void onItemSelected(AdapterView<?> arg0, View arg1, int arg2,
				long arg3) {
			// System.out.println(spinner.getSelectedItem().toString());
		}

		@Override
		public void onNothingSelected(AdapterView<?> arg0) {

		}

	}
}
